import axios from 'axios';
import { Message } from "element-ui"
// import { reciveBaseUrl } from '@/utils/config.js'
import router from '../router';
// import Store from "@/store/index"
// // 封装服务器地址
// axios.defaults.baseURL = reciveBaseUrl;
axios.defaults.baseURL = 'http://43.139.121.71:81/'; //这是真是环境啊

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  config.data = config.data || ''
  if (config.auth == true) {
    let token = { token: localStorage.getItem('token') }
    config.data = { ...config.data, ...token }
  };
  return config;
}, function (error) {
  return Promise.reject(error);
});


// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  let { data } = response
  // console.log(data);
  // 错误代码
  if (data.code != 1) {
    Message({
      type: "error",
      message: data.msg
    })
    if (data.code == 3) {
      router.push({ 'path': '/user/login' })
    }
    return new Promise(() => { })
  }
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});

// 暴露axios
export default axios